/**
 * kaGridColsTemplet 封装
 * 
 * 版本：v1.0
 * 
 * Stanley
 **/
layui.define(['jquery'],
    function (exports) {
        var $ = layui.$;
        var kaGridColsTemplet = new function () {
            this.init = function (params, templetType) { // 初始化
                if (templetType != undefined) {
                    if (templetType.indexOf("customize_") > -1) { // 自定义
                        var templet = function (d) {
                            var arry = templetType.split("_");
                            var customizeHtml = layui.kaGridColsTemplet.customize(d, "customize", arry[1], this.field);
                            return customizeHtml;
                        }
                        return templet;
                    } else if (templetType.indexOf("dic_") > -1) { // 字典
                        var templet = function (d) {
                            var arry = templetType.split("_");
                            var customizeHtml = layui.kaGridColsTemplet.customize(d, "dic", arry[1], this.field);
                            return customizeHtml;
                        }
                        return templet;
                    } else if (templetType.indexOf("button_") > -1) { // 按钮
                        var templet = function (d) {
                            var arry = templetType.split("_");
                            var customizeHtml = layui.kaGridColsTemplet.customize(d, "button", arry[1], this.field);
                            return customizeHtml;
                        }
                        return templet;
                    }
                }
            }
            this.customize = function (d, type, arry1, field) {
                var customizeHtml = d[field];
                var ps = top[arry1 + "_" + type + "_colsTempletData"];
                if(ps == undefined || ps == null){
                    $.ajax({
                        url: "../kaGrid/cols/" + type + "/" + arry1 + ".js",
                        type: "GET",
                        dataType: "script",
                        async: false, //同步
                        success: function () {
                            var pageEventsData = eval(arry1 + "_" + type + "_colsTempletData");
                            if (pageEventsData != undefined) {
                                top[arry1 + "_" + type + "_colsTempletData"] = pageEventsData;
                                customizeHtml = pageEventsData.init(d);
                            } else {
                                customizeHtml = d[d.LAY_COL.field];
                            }
                        },
                        timeout: 5000 // 5秒超时
                    });
                } else {
                    customizeHtml = ps.init(d);
                }
                return customizeHtml;
            }
            this.loadButtonClick = function (buttonFunUrl) {
                var ps = top[buttonFunUrl + "_ButtonFunData"];
                if (ps == undefined || ps == null) {
                    $.ajax({
                        url: "../kaGrid/cols/buttonfun/" + buttonFunUrl + ".js",
                        type: "GET",
                        dataType: "script",
                        async: false, //同步
                        success: function () {
                            var pageEventsData = eval(buttonFunUrl + "_ButtonFunData");
                            if (pageEventsData != undefined) {
                                top[buttonFunUrl + "_ButtonFunData"] = pageEventsData;
                            }
                        },
                        timeout: 5000 // 5秒超时
                    });
                }
            }
        };
        exports('kaGridColsTemplet', kaGridColsTemplet);
    }
)